package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Car;
import cn.wolfcode.crm.query.CarQueryObject;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("car")
public class CarController {

    @Autowired
    private ICarService carService;
    /**
     * 车辆信息页面
     * @return
     */
    @RequestMapping("")
    public String carPage(){
        return "/car/list";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(CarQueryObject qo){
        return carService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions("car:saveOrUpdate")
    @PermissionName("车辆保存与修改")
    public JsonResult saveOrUpdate(Car car){
        JsonResult result = new JsonResult();
        try{
            carService.saveOrUpdate(car);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("car:delete")
    @PermissionName("车辆删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try{
            carService.deleteByPrimaryKey(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

    @RequestMapping("selectCarByBillId")
    @ResponseBody
    public Car selectCarByBillId(Long id){

        return carService.selectCarByBillId(id);
    }
}
